import pymysql

class DB():
    def __init__(self,host='localhost',user='root',port=3306,password='72305392^)f',database='aiparents', charset='utf8'):
        # 建立连接
        self.conn = pymysql.connect(host=host, port=port, database=database, user=user, passwd=password, charset=charset)
        # 创建游标
        self.course = self.conn.cursor(cursor = pymysql.cursors.DictCursor)

    def __enter__(self):
        # 返回游标
        return self.course
    def __exit__(self, exc_type, exc_val, exc_tb):
        # 提交数据库并执行
        self.conn.commit()
        # 关闭游标
        self.course.close()
        # 关闭数据库连接
        self.conn.close()


def run(classes=[]):
    print("开始")
    with DB(host='localhost', user='root', port=3306, password='72305392^)f', database='aiparents', charset='utf8') as db:
        db.execute("select * from course ORDER BY id desc")
        for iq in db:
            print(iq)

if __name__ == '__main__':
    # with DB(host='localhost',user='root',port=3306,password='72305392^)f',database='aiparents', charset='utf8') as dbf:
    #     dbf.execute("select * from course ORDER BY id desc")
    #     for i in dbf:
    #         print(i)
    run()